package cn.itsource.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.itsource.mapper.DriverMaterialAuthLogMapper;
import cn.itsource.mapper.DriverSettingMapper;
import cn.itsource.mapper.DriverSummaryMapper;
import cn.itsource.pojo.domain.DriverAuthMaterial;
import cn.itsource.mapper.DriverAuthMaterialMapper;
import cn.itsource.pojo.domain.DriverMaterialAuthLog;
import cn.itsource.pojo.domain.DriverSetting;
import cn.itsource.pojo.domain.DriverSummary;
import cn.itsource.pojo.dto.AuditDto;
import cn.itsource.pojo.vo.GetStataVo;
import cn.itsource.result.JSONResult;
import cn.itsource.service.IDriverAuthMaterialService;
import cn.itsource.service.IDriverMaterialAuthLogService;
import cn.itsource.utils.IdUtil;
import cn.itsource.utils.RConst;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 司机实名资料 服务实现类
 * </p>
 *
 * @author tt
 * @since 2024-08-31
 */
@Service
public class DriverAuthMaterialServiceImpl extends ServiceImpl<DriverAuthMaterialMapper, DriverAuthMaterial> implements IDriverAuthMaterialService {
    @Autowired
    private DriverMaterialAuthLogMapper  driverMaterialAuthLogMapper;

    @Autowired
    private DriverSummaryMapper driverSummaryMapper;

    @Autowired
    private DriverSettingMapper driverSettingMapper;

    @Override
    public boolean save(DriverAuthMaterial entity) {
        int count = this.count(new LambdaQueryWrapper<DriverAuthMaterial>()
                .eq(DriverAuthMaterial::getDriverId, entity.getDriverId())
                .ne(DriverAuthMaterial::getRealAuthStatus, RConst.Num.INT_2));
        if(count==RConst.Num.INT_0){
            entity.setId(IdUtil.getSnowFlackId());
            entity.setCreateTime(new Date());
            entity.setRealAuthStatus(RConst.Num.INT_0);
            return this.save(entity);
        }
        return false;
    }

    /**
     * 审核司机信息
     * */
    @Override
    public JSONResult audit(AuditDto auditDto) {
        DriverAuthMaterial driverAuthMaterial = this.getOne(new LambdaQueryWrapper<DriverAuthMaterial>()
                .eq(DriverAuthMaterial::getId, auditDto.getDriverId())
                .eq(DriverAuthMaterial::getRealAuthStatus, RConst.Num.INT_0));
        if(auditDto.getApprove()){
            //修改审核人信息
            driverAuthMaterial.setAuditTime(new Date());
            driverAuthMaterial.setRealAuthStatus(RConst.Num.INT_1);
            driverAuthMaterial.setAuditUserId(auditDto.getLogin().getId());
            driverAuthMaterial.setAuditRemark(auditDto.getRemark());
            //修改数据库里的司机信息
            this.updateById(driverAuthMaterial);
            //添加日志表
            extracted(auditDto, RConst.Num.INT_1);

            //初始化司机订单信息
            DriverSummary driverSummary1 = new DriverSummary();
            driverSummary1.setId(Long.valueOf(driverAuthMaterial.getDriverId()));
            driverSummary1.setCreateTime(new Date());
            driverSummaryMapper.insert(driverSummary1);

            //初始化司机的配置信息
            DriverSetting driverSetting = new DriverSetting();
            driverSetting.setId(Long.valueOf(driverAuthMaterial.getDriverId()));
            driverSetting.setCreateTime(new Date());
            driverSetting.setAutoAccept(true);
            driverSetting.setOrientation(true);
            driverSetting.setListenService(true);
            driverSetting.setRangeDistance(50);
            driverSetting.setOrderDistance(50);
            driverSettingMapper.insert(driverSetting);

            return JSONResult.success("申请通过");
        }else {
            //修改审核人信息
            driverAuthMaterial.setAuditTime(new Date());
            driverAuthMaterial.setRealAuthStatus(RConst.Num.INT_2);
            driverAuthMaterial.setAuditUserId(auditDto.getLogin().getId());
            driverAuthMaterial.setAuditRemark(auditDto.getRemark());
            //修改数据库里的司机信息
            this.updateById(driverAuthMaterial);
            //添加日志表
            extracted(auditDto, RConst.Num.INT_2);
            return JSONResult.success("申请驳回");
        }
    }

    /**
     * 撤销审核
     * */

    @Override
    public JSONResult cancel(AuditDto auditDto) {
        DriverAuthMaterial driverAuthMaterial = this.getOne(new LambdaQueryWrapper<DriverAuthMaterial>()
                .eq(DriverAuthMaterial::getId, auditDto.getDriverId()));
        if(auditDto.getApprove()){
            //修改审核人信息
            driverAuthMaterial.setAuditTime(new Date());
            driverAuthMaterial.setRealAuthStatus(RConst.Num.INT_0);
            driverAuthMaterial.setAuditUserId(auditDto.getLogin().getId());
            driverAuthMaterial.setAuditRemark(auditDto.getRemark());
            //修改数据库里的司机信息
            this.updateById(driverAuthMaterial);
            //添加日志表
            extracted(auditDto, RConst.Num.INT_0);
            //删除司机订单信息
            //先查询数据库中有没有该司机的订单信息
            DriverSummary driverSummary = driverSummaryMapper.selectOne(new LambdaQueryWrapper<DriverSummary>()
                    .eq(DriverSummary::getId,Long.valueOf(driverAuthMaterial.getDriverId())));
            if(driverSummary!=null){
                driverSummaryMapper.del(driverSummary.getId());
            }
            //删除司机配置信息
            DriverSetting driverSetting = driverSettingMapper.selectOne(new LambdaQueryWrapper<DriverSetting>()
                    .eq(DriverSetting::getId, Long.valueOf(driverAuthMaterial.getDriverId())));
            if(driverSummary!=null){
                driverSettingMapper.del(driverSetting.getId());
            }
            return JSONResult.success("取消成功");
        }
        return JSONResult.error("操作失败");
    }

    /**
     * 获取审核状态(做状态回显)
     * */
    @Override
    public JSONResult getStatus(Long id) {
        DriverAuthMaterial driverAuthMaterial1 = this.getOne(new LambdaQueryWrapper<DriverAuthMaterial>()
                .eq(DriverAuthMaterial::getDriverId, id)
                .ne(DriverAuthMaterial::getRealAuthStatus, RConst.Num.INT_2));
        if(driverAuthMaterial1!=null){
            return JSONResult.success(driverAuthMaterial1);
        }
        return JSONResult.error();
    }

    /**
     * 获取审核状态(做页面跳转)
     * */
    @Override
    public JSONResult getDriverStatus() {
        //获取当前登录人的id
        Long driverId = StpUtil.getLoginIdAsLong();
        System.out.println(driverId);
        DriverAuthMaterial driverAuthMaterial = this.getOne(new LambdaQueryWrapper<DriverAuthMaterial>()
                .eq(DriverAuthMaterial::getDriverId, driverId)
                .eq(DriverAuthMaterial::getRealAuthStatus, RConst.Num.INT_1));
        if(driverAuthMaterial!=null){
            DriverSummary driverSummary = driverSummaryMapper.selectOne(new LambdaQueryWrapper<DriverSummary>()
                    .eq(DriverSummary::getId, driverId));
            GetStataVo getStataVo = new GetStataVo();
            getStataVo.setRealAuthSuccess(true);
            getStataVo.setTodayIncome(driverSummary.getTodayIncome());
            getStataVo.setDriveDuration(driverSummary.getDriveDuration());
            getStataVo.setTodayTradeOrders(driverSummary.getTodayTradeOrders());
            return JSONResult.success(getStataVo);
        } else {
            GetStataVo getStataVo = new GetStataVo();
            getStataVo.setRealAuthSuccess(false);
            return JSONResult.success(getStataVo);
        }

    }

    /**
    * 添加日志表方法（提出来的）
    * */
    private void extracted(AuditDto auditDto, int status) {
        DriverMaterialAuthLog log = new DriverMaterialAuthLog();
        log.setId(IdUtil.getSnowFlackId());
        log.setAuthMaterialId(Long.valueOf(auditDto.getDriverId()));
        log.setRealAuthStatus(status);
        log.setAuditTime(new Date());
        log.setAuditUserId(auditDto.getLogin().getId());
        log.setAuditRemark(auditDto.getRemark());
        log.setCreateTime(new Date());
        driverMaterialAuthLogMapper.insert(log);
    }

}
